#!/usr/bin/env python3
# encoding: utf-8
"""
threshold.py

Created by Dmitry Tsybin on 2011-03-07.
Copyright (c) 2011 All rights reserved.
"""

import sys
import os
import unittest
from constants import *

class Threshold:
	'''Класс для использования порогового метода'''
	
	def __init__(self):
		self.meanValue = 0
		self.threshold = 0.1
		self.counter = 0
		
	def calculate(self, nowValue):
		self.meanValue = (self.meanValue * self.counter + nowValue) / (self.counter + 1)
		self.counter += 1
		
	def discord(self, nowValue, threshold = 0.1):
		'''функция возвращает STATUS'''

		self.calculate(nowValue)
		
		if threshold < 0:
			raise
		
		if self.meanValue < threshold:
			res = STATUS.ok
			
		elif self.meanValue < 0.5:
			res = STATUS.discord

		else:
			res = STATUS.crash
				
		return res
		

class ThresholdTest(unittest.TestCase):
	knownValues1 = [ (0.05, 0.1, STATUS.ok),
					(0.09, 0.1, STATUS.ok),
					(0.1, 0.1, STATUS.discord),
					(0.3, 0.1, STATUS.discord),
					(0.5, 0.1, STATUS.crash),
					(0.9, 0.1, STATUS.crash),
					(1.0, 0.1, STATUS.crash) ]
				
	knownValues2 = [ (0), (0.01), (0), (0), (0.01) ]
							
	def testThreshold1(self):
		for nowVal, thr, status in self.knownValues1:
			testUnit = Threshold()
			result = testUnit.discord(nowVal, thr)
			self.assertEqual(status, result)
			
	def testThreshold2(self):
		testUnit = Threshold()
		for nowVal, thr, status in self.knownValues1:
			result = testUnit.discord(nowVal, thr)
		self.assertEqual(STATUS.discord, result)
		
	def testThreshold3(self):
		testUnit = Threshold()
		for nowVal in self.knownValues2:
			result = testUnit.discord(nowVal)
		self.assertEqual(STATUS.ok, result)


if __name__ == '__main__':
	unittest.main()
	 